<?php



class Reg extends MY_Controller
{
	function index(){
		
		$this->load->model('User_model');
		$this->smarty->assign("phone_has", 0);
        $post_data = $this->input->post();
        if($post_data){
            $has_error = 0;
            if(empty($post_data['phone'])){
                $this->smarty->assign("phone_err", 1);
                $has_error = 1;
            }else{
                $this->smarty->assign("phone", $post_data['phone']);

                //已经填写手机号、验证用户是否存在
                $user = $this->User_model->get_user_by_phone($post_data['phone']);
                if(empty($user)){
                    //用户不存在，验证密码是否填写
                    if(empty($post_data['password'])){
                        $this->smarty->assign("password_err", 1);
                        $has_error = 1;
                    }
                 
                    
                }else $this->smarty->assign("phone_has", 1);
            }
            if(empty($post_data['vcode'])){
                $this->smarty->assign("vcode_err", 1);
                $has_error = 1;
            }

            //判断验证码是否正确
            if(empty($post_data['phone'])==false && empty($post_data['vcode'])==false){
                //查询用户最后一条code
                $this->load->model("Verification_code");
                $code_model = $this->Verification_code->getLastCodeByPhone($post_data['phone'],1);
                if($code_model['is_use']==1||$code_model['code']!=$post_data['vcode']){
                    //验证码错误
                    $this->smarty->assign("vcode_err", 1);
                    $has_error = 1;
                }
            }
            
            if($has_error == 0){
				$md5_pass = md5($post_data['password']);
				$rand_str = '';
				for($i=0; $i<10; $i++){
					$rand_str .= substr($md5_pass, rand(0, strlen($md5_pass)-1), 1);
				}

				$password = md5($md5_pass.$rand_str);
				$register_ip = $_SERVER["REMOTE_ADDR"];

                //添加注册
                $result = $this->User_model->reg_user($post_data['phone'],$rand_str,$password,$register_ip);
                if($result > 0) {
                    // reg_wecenter($post_data['phone'], $post_data['password'], $post_data['phone']."@qq.com");
                    header("Location:/login");exit;
                }
            }

        
		
		}
		$this->smarty->display("reg.html");
	}



     
    function reg_msg($phone){
        //查询手机用户
        $this->load->model('User_model');
        $user = $this->User_model->get_user_by_phone($phone);
        if(empty($user)==false){
                print_r('{"code":300,"msg":"您已经注册过了，请登录"}');
                exit ;
        }
        $this->load->helper('common');
        //查询用户最后一条code
        $this->load->model("Verification_code");
        $code_model = $this->Verification_code->getLastCodeByPhone($phone,1);
        if(empty($code_model)|| strtotime($code_model['create_on'])+1*60<time()){
            $code = getRandChar(4);
            $result = $this->Verification_code->insertCodeLog($phone,$code,1);
            if($result>0){
                $send_result = send_verify_code_msg($phone,$code);
                if(empty($user)){
                    //未注册，未开店
                    print_r($send_result);exit;
                }else{
                    //已注册、未开店
//                    print_r(json_decode($send_result));
                    if(json_decode($send_result)->code==200){
                        print_r('{"code":201,"msg":"已注册、未开店"}');exit;
                    }else{
                        print_r($send_result);exit;
                    }
                }
                return ;
            }else{
                print_r('{"code":400,"msg":"系统异常"}');
                return ;
            }
        }else{
            print_r('{"code":500,"msg":"您操作太频率了！请稍后"}');
            return ;
        }

    }


} 